//
//  LZFinanicalOneMonthRecordsViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/23.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZFinanicalOneMonthRecordsViewController.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "MJRefresh.h"
#import "LZCouponOrderTableCell.h"
#import "LZFinancialPayedModel.h"
#import "LZFinancialPresenter.h"
#import "LZFinancialDetailViewController.h"
#import "LZGrabCouponDetailViewController.h"
#import "LZFightGroupDetailViewController.h"
#import "LZOrderModel.h"
#import "UITableView+LZTableView.h"
#import "LZDateFormatterManager.h"


@interface LZFinanicalOneMonthRecordsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UILabel *bigTitlePriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *titlePriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *financialPeriodLabel;
@property (weak, nonatomic) IBOutlet UIButton *provisionDetailBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *provisionDetailBtnWidth;

@property (assign, nonatomic) NSInteger currentPage;

@property (strong, nonatomic) LZFinancialPresenter *financialPresenter;
@property (strong, nonatomic) NSMutableArray *dataArray;
@end

@implementation LZFinanicalOneMonthRecordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.umLogPageViewName = @"当前一个月的结款记录页面";
    
    [self configureUI];
    
    [self configureData];
    
    [self getListDataWithPage:1];
}

#pragma mark -
#pragma mark private
- (void)upLoadMoreVideoListData
{
    [self getListDataWithPage:self.currentPage];
}

- (void)getListDataWithPage:(NSInteger)cPage
{
    WS(wSelf);
    [self.financialPresenter getFinanicalOneMonthRecordWithPayedID:self.payedModel.payedID CurrentPage:cPage Limit:10 resultSuccess:^(NSArray *listArr, NSInteger totalPage) {
        
        if (totalPage > cPage) {
            [wSelf.tableView.mj_footer endRefreshing];
        }else{
            [wSelf.tableView setNoMoreDataFooterText];
            [wSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        if (listArr.count > 0) {
            [wSelf.dataArray addObjectsFromArray:listArr];
            [wSelf.tableView reloadData];
        }
        self.currentPage ++;
        
    } Failure:^(NSString *fail) {
        
    }];
}

- (IBAction)provisionDetailBtnAction:(id)sender {
    LZFinancialDetailViewController *vc = [[LZFinancialDetailViewController alloc] init];
    vc.payID = self.payedModel.payedID;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -
#pragma mark ui
- (void)configureUI{
    
    self.title = LZLocalizedString(@"provision_details", nil);
    
    _provisionDetailBtn.layer.cornerRadius = 20.0;
    _provisionDetailBtn.layer.masksToBounds = YES;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LZCouponOrderTableCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.tableHeaderView = self.topView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMoreVideoListData)];
    [self.tableView configFooterRefreshText];
}

- (void)configureData
{
    _currentPage = 1;
    _dataArray = @[].mutableCopy;
    _financialPresenter = [[LZFinancialPresenter alloc] init];
    
    GET_SCREEN_SCALE(scale);
    _provisionDetailBtnWidth.constant = 275*scale;
    
    self.titlePriceLabel.text = [NSString stringWithFormat:@"%@ %.3fBHD",LZLocalizedString(@"provision_amount", nil),self.payedModel.payMoney];
    self.financialPeriodLabel.text = [NSString stringWithFormat:@"%@ %@",LZLocalizedString(@"account_period", nil),[[LZDateFormatterManager shareInstance] stringFromStringNormalHaveLineyyyyMM:self.payedModel.circle]];
    [self.provisionDetailBtn setTitle:LZLocalizedString(@"provision_details", nil) forState:UIControlStateNormal];
    
    NSString *price = [NSString stringWithFormat:@"%.3f",self.payedModel.payMoney];
    NSString *str = @"BHD";
    NSString *showStr = [NSString stringWithFormat:@"%@%@",price,str];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:showStr];
    [attStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:24] range:NSMakeRange(0, price.length)];
    self.bigTitlePriceLabel.attributedText = attStr;
}

#pragma mark -
#pragma mark tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WS(wSelf);
    return [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(id cell) {
        LZCouponOrderTableCell *cellTemp = (LZCouponOrderTableCell *)cell;
        LZOrderModel *m = wSelf.dataArray[indexPath.row];
        cellTemp.orderModel = m;
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LZCouponOrderTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    LZOrderModel *m = self.dataArray[indexPath.row];
    cell.orderModel = m;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LZOrderModel *modelTemp = self.dataArray[indexPath.row];
    if ([modelTemp.promotionType isEqualToString:@"1"]) {//优惠券
        LZGrabCouponDetailViewController *vc = [[LZGrabCouponDetailViewController alloc] init];
        vc.couponId = modelTemp.promotionId;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        LZFightGroupDetailViewController *vc = [[LZFightGroupDetailViewController alloc] init];
        vc.couponId = modelTemp.promotionId;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

@end
